package com.he.fetch.test.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "ACCOUNT")
public class Account implements java.io.Serializable {

    private static final long serialVersionUID = 1658539316700444916L;

    @ManyToOne
    @JoinColumn(name = "BACKUP_ID")
    private User backup;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID", unique = true, nullable = false)
    private Integer id;

    @ManyToOne
    @JoinColumn(name = "MANAGER_ID")
    private User manager;

    @Column(name = "NAME", unique = true, nullable = false, length = 255)
    private String name;

    @Column(name = "IS_PRIMARY", nullable = false)
    private boolean primary;

    @OneToMany(mappedBy = "account", cascade = CascadeType.ALL)
    private Set<Txn> txns = new HashSet<Txn>();

    @ManyToOne
    @JoinColumn(name = "ACCOUNT_TYPE_ID")
    private AccountType type;

    public boolean add(Txn txn) {
	boolean added = false;
	if (txn != null) {
	    added = this.txns.add(txn);
	    if (added) {
		txn.setAccount(this);
	    }
	}
	return added;
    }

    public User getBackup() {
	return this.backup;
    }

    public Integer getId() {
	return this.id;
    }

    public User getManager() {
	return this.manager;
    }

    public String getName() {
	return this.name;
    }

    public Set<Txn> getTxns() {
	return this.txns;
    }

    public AccountType getType() {
	return this.type;
    }

    public boolean isPrimary() {
	return this.primary;
    }

    public void setBackup(User backup) {
	this.backup = backup;
    }

    public void setId(Integer id) {
	this.id = id;
    }

    public void setManager(User manager) {
	this.manager = manager;
    }

    public void setName(String name) {
	this.name = name;
    }

    public void setPrimary(boolean primary) {
	this.primary = primary;
    }

    public void setTxns(Set<Txn> txns) {
	this.txns = txns;
    }

    public void setType(AccountType type) {
	this.type = type;
    }

    @Override
    public String toString() {
	return "[id=" + this.id + ", name=" + this.name + "]";
    }

}
